#!/bin/ash -efu
#
# mki-copy-e2kboot
#
# This file is part of mkimage
# Copyright (C) 2008  Michael A. Kangin <mak@rsmu.ru>
# Copyright (C) 2008-2020  Alexey Gladkov <gladkov.alexey@gmail.com>
# Copyright (C) 2018  Michael Shigorin <mike@altlinux.org>
#
# This file is covered by the GNU General Public License,
# which should be included with mkimage as the file COPYING.
#

. "${0%/*}"/mki-sh-functions

verbose "has started executing."

[ -d "$chroot" ] ||
	fatal "$dir: does not look like a hasher work directory."

run_chrooted "$chroot" <<EOF
#!/bin/sh -efu

mkdir -p /.image/alt0

if [ -L /boot/image ]; then
	kimage="\$(readlink -ev /boot/image)"
else
	kimage="\$(find /boot -type f -name 'image-*' -print -quit)"
fi

if [ -z "\$kimage" ]; then
	echo "no kernel, no initrd, no boot.conf => not bootable!" >&2
	exit 1
fi

cp $verbose -aLf "\$kimage" /.image/alt0/vmlinux.0
if [ -f /boot/full.cz ]; then
	cp $verbose -aLf /boot/full.cz /.image/alt0/full.cz
fi

# should be prepared by the profile normally; this handles altinst only
[ -s /boot/boot.conf ] &&
	cp $verbose -aLf /boot/boot.conf /.image/boot.conf ||
	cat > /.image/boot.conf <<BOOTCONF
default=install
timeout=3

label=install
    partition=0
    image=/alt0/vmlinux.0
    cmdline=console=ttyS0,115200 console=tty0 hardreset fastboot live automatic=method:cdrom ${BOOT_LANG:+lang=${BOOT_LANG-}}
    initrd=/alt0/full.cz
BOOTCONF
EOF
